const path = require("path");
// require('events').EventEmitter.defaultMaxListeners = 20;
const FileManagerPlugin = require('filemanager-webpack-plugin'); //压缩dist文件夹
const args = require("minimist")(process.argv.slice(2));
process.env.BASE_URL = "";
const isDev = process.env.NODE_ENV === "development";

const mockSign = args.mock === "true";
const isProBuilding = args.buildModel === 'prod';

if (mockSign) {
    process.env.VUE_APP_MOCK = "true";
    process.env.VUE_APP_MOCK_MODULES = args.mockModule || "";
}

// 环境模块配置===
const clientId = "ucfront";
// const fileBasePath = "http://10.21.19.51:8888/";
const moduleMap = {
    sso: { urlPerfix: "/sso" },
    sam: { urlPerfix: "/sam" },
    // ucfront: { urlPerfix: "/ucfront" },
    oa:{urlPerfix: "/oa"},
    textVoice: {
        urlPerfix: "/textVoice"
    },
    voice: {
        urlPerfix: "/voice"
    }
};
const devBaseUrl =
"http://127.0.0.1:8080"

const wsOrigin = "ws://10.170.13.161:8000"; // ws地址


// 常量定义===
const provCode = "00030000"; // 00030000
const VUE_APP_PREFIX = "/api";

// ===环境配置
// process.env.VUE_APP_FILE_BASE_PATH = fileBasePath;
process.env.VUE_APP_IS_DEV = isDev;
process.env.PROBUILDING = isProBuilding; // 生产打包模式
process.env.VUE_APP_CLIENT_ID = clientId;
process.env.VUE_APP_MODULE_CONFIG = JSON.stringify(moduleMap);
process.env.VUE_APP_PROV_CODE = provCode;
process.env.VUE_APP_PREFIX = VUE_APP_PREFIX;


const devProxySet = {
    target: devBaseUrl,
    /* 新增 */
    changeOrigin: true,
    secure: false,
};

const moduleProxy = {};

Object.keys(moduleMap).forEach((moduleName) => {
    const moduleItem = moduleMap[moduleName];
    moduleProxy['^' + moduleItem.urlPerfix] = devProxySet;
});

const packName = isProBuilding ? 'prodPack' : 'testPack'
const onEnd = isDev ? {} : {
    copy: [{ source: './front', destination: `./${packName}/temp/front` }],
    archive: [{ source: `./${packName}/temp`, destination: `./${packName}/front.zip` }],
    delete: [ `./${packName}/temp`]
}

//拼接路径
function resolve(dir) {
    return path.join(__dirname, dir);
}
module.exports = {
    publicPath: "./",
    lintOnSave: false,
    chainWebpack: (config) => {
        config.resolve.alias
            .set("@", resolve("src"))
            .set("@assets", resolve("src/assets"))
            .set("@components", resolve("src/components"))
            .set("@static", resolve("src/static"))
            .set("@views", resolve("src/views"))
            .set("@service", resolve("src/service"))
            .set("@utils", resolve("src/utils"))
            .set("@public", resolve("public"))
            .set("@constants", resolve("src/constants"));
        config.module
            .rule('excel')
            .test(/\.(xlsx|xls)$/)
            .use('url-loader')
            .loader('url-loader')
            .tap(options => {
                return {
                    ...options,
                    name: 'static/excel/[name].[ext]',
                    limit: 10,
                }
            })
            .end()

        config.plugin('define')
            .tap(args => {
                args[0] = {
                    ...args[0],
                    VUE_API_VERSION0: '"1.0.1"',
                    VUE_API_VERSION1: '"1.1.1"',
                    VUE_API_KEY: '"0d1a1d88cb6643aa3838e9dcaf9738f5"',
                    isDev: `${isDev}`,

                }
                return args;
            })
    },
    configureWebpack: () => {
        const videoUrl = "https://webrtc.asiainfo-isac.com:10443";
        const proxy = {
            '^/ChannelControl': {
                target: wsOrigin,
                ws: true,
                changeOrigin: true
            },
            '^/socket.io': {
                target: wsOrigin,
                ws: true,
                changeOrigin: true
            },
            "^/mock": {
                target: `http://localhost:3618`,
                // pathRewrite: { [`^/mock`]: "" },
                changeOrigin: true,
                secure: false,
                pathRewrite: (url) => {
                    console.log("/mock",url,url.replace('/mock',''))
                    return url.replace('/mock','');
                }
            },
            [`^${VUE_APP_PREFIX}`]: {
                ...devProxySet,
                pathRewrite: { [`^${VUE_APP_PREFIX}`]: "" },
            },
            "/yn_sr":{
                ...devProxySet,
                pathRewrite: (url) => {
                    return url.replace('/yn_sr','');
                },
            },
        };
        return {
            // devtool: isDev ? "source-map" : false,
            devtool: isDev ? "cheap-module-eval-source-map" : false,
            devServer: {
                open: true,
                host: "0.0.0.0",
                port: 3333,
                https: false,
                hotOnly: false,
                disableHostCheck: true,
                proxy,
                // before: (app) => {},
                overlay: {
                    warnings: false,
                    errors: true,
                },
                // 服务器允许跨域访问操作(主要为了解决远程调试跨域问题)
                headers: {
                    "Access-Control-Allow-Origin": "*",
                    "Access-Control-Allow-Headers": "*",
                },
            },
            plugins: [new FileManagerPlugin({ events: { onEnd } })]
        };
    },
};
